class Play extends eui.Component implements eui.UIComponent {
	public constructor() {
		super();
		this.skinName = PlaySkin;
	}

	protected partAdded(partName: string, instance: any): void {
		super.partAdded(partName, instance);
	}

	private lotto;		//立即抽奖
	private playAgain;	//再来一次
	private yes;
	private no;
	private user;		//深圳弹出框动画
	private gameOver;	//结束弹出框动画
	private tennic;		// 网球
	private kmPlaying;	//游戏中的km数
	private kmResult;	//结束后的km数
	private result;
	private userType;
	private playing;
	private bgTween;

	private timer1;		//定时器
	private timer2;
	private timer3;
	private timer4;

	private rain;		//雨
	private bottle;		//矿泉水瓶子
	private ran1;		//随机数
	private ran2;
	private offsetX;
	private cheerText;
	private sound:egret.Sound;
	private failed:egret.Sound;
	private soundChannel:egret.SoundChannel;

	protected childrenCreated(): void {
		super.childrenCreated();

		//音乐
		this.sound = RES.getRes("youxibeijing_mp3"); 
		this.failed = RES.getRes("shibai_mp3");	
        this.soundChannel = this.sound.play();

		this.bgTween = egret.Tween.get(this.playing,{loop:true}).to({y:3754},10000);
		this.bgTween.setPaused(false);

		//马上抽奖事件
		this.lotto.once(egret.TouchEvent.TOUCH_TAP, () => {
			this.user.play();
			this.swapChildren(this.userType,this.result);
			this.playAgain.touchEnabled = false;
		})

		//再玩一次事件
		this.playAgain.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
			SceneManager.createObj(this.stage).loadScene("index", Index);
		})

		//点击"是"按钮
		this.yes.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
			SceneManager.createObj(this.stage).loadScene("share", Share);
		})
		//点击"否"按钮
		this.no.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
			SceneManager.createObj(this.stage).loadScene("share", Share);
		})

		this.rain = new egret.Bitmap();
		this.bottle = new egret.Bitmap();
		//随机生成雨
		this.timer1 = new egret.Timer(1500, 0);
		this.timer1.addEventListener(egret.TimerEvent.TIMER, () => {
			this.rain.texture = RES.getRes("game-yu_png");
			this.addChild(this.rain);
			this.rain.width = 200;
			this.rain.height = 200;
			this.rain.y = -200;
			this.rain.x = this.ran1;
		})
		this.timer1.start();

		//随机生产瓶子
		this.timer2 = new egret.Timer(3500, 0);
		this.timer2.addEventListener(egret.TimerEvent.TIMER, () => {
			this.bottle.texture = RES.getRes("pingzi_png");
			this.addChild(this.bottle);
			this.bottle.width = 100;
			this.bottle.height = 240;
			this.bottle.y = -240;
			this.bottle.x = this.ran2;
		})
		this.timer2.start();

		//通过随机数改变雨，瓶子在x轴的位置
		this.timer3 = new egret.Timer(10, 0);
		this.timer3.addEventListener(egret.TimerEvent.TIMER, function () {
			this.ran1 = Math.floor(Math.random() * 440);
			this.ran2 = Math.floor(Math.random() * 540);
		}, this);
		this.timer3.start();

		this.timer4 = new egret.Timer(10, 0);
		this.timer4.addEventListener(egret.TimerEvent.TIMER, () => {
			this.rain.y += 15;
			this.bottle.y += 10;
		})
		this.timer4.start();

		//距离
		this.kmPlaying.text = StartGame.initKm;
		this.kmPlaying.addEventListener(egret.Event.ENTER_FRAME,this.grade,this);

		//移动球
		this.tennic.touchEnable = true;
		this.tennic.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.startMove, this);
		this.tennic.addEventListener(egret.TouchEvent.TOUCH_END, this.stopMove, this);

		// this.tennic.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.move, this);

		//碰撞检测
		this.addEventListener(egret.Event.ENTER_FRAME, this.crash, this);

		this.cheerText = new egret.TextField();
		this.cheerText.text = "+30km！";
		this.cheerText.width = 200;
		this.cheerText.textAlign = "center";
		this.cheerText.x = 220;
		this.cheerText.y = 50;
		this.cheerText.textColor = 0xffff00;
		this.cheerText.size = 36;
		this.addChild(this.cheerText);
		this.cheerText.visible = false;

	}

	private startMove(e) {
		//把手指按到的对象记录下来
		console.log(e);
		this.tennic = e.currentTarget;
		//计算手指和要拖动的对象的距离
		this.offsetX = e.stageX - this.tennic.x;
		//手指在屏幕上移动，会触发 onMove 方法
		this.tennic.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onMove, this);
	}
	private stopMove() {
		this.tennic.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onMove, this);
	}
	private onMove(e) {
		var n = e.stageX - this.offsetX;
		if (n >= 0 && n <= this.stage.stageWidth - this.tennic.width) {
			this.tennic.x = n;
		}
	}

	//分数
	private grade(){
		var begin = this.kmPlaying.text;
		begin++;
		this.kmPlaying.text = begin;
		this.kmResult.text = this.kmPlaying.text;
	}

	private isCli =false;
	private isCrash = false;
	//碰撞


	private crash() {
		if (!this.isCli&&this.isCollsion()) {
			this.isCli = true;
			let addAfterKm = parseInt(this.kmPlaying.text);
			if(this.contains(this.bottle)){
				this.removeChild(this.bottle);
				addAfterKm += 30;
			}
			this.kmPlaying.text = addAfterKm;
			this.kmResult.text = this.kmPlaying.text;
			this.cheerText.visible = true;
			setTimeout(()=>{
				this.cheerText.visible = false;
			},300)
		}else{
			this.isCli = false;
		}

		if (!this.isCrash&&this.isCollsionWithRect()) {
			this.isCrash = true;
			this.soundChannel.stop();
			this.failed.play(0,1);
			this.timer1.stop();
			this.timer2.stop();
			this.gameOver.play();
			this.bgTween.setPaused(true);
			this.kmPlaying.removeEventListener(egret.Event.ENTER_FRAME,this.grade,this);
			this.setChildIndex(this.tennic,1);
			if(this.contains(this.bottle)){
				this.removeChild(this.bottle);
			}
		}else{
			this.isCrash = false;
		}
	}

	//球跟雨的碰撞
	private isCollsionWithRect() {
		var t = this.tennic;
		var a = this.rain;
		if(t.x+t.width>a.x&&t.x<a.x+a.width&&t.y+t.height>a.y&&t.y<a.y+a.width){
			return true;
		}
		return false;

	}
	//球跟瓶子的碰撞
	private isCollsion() {
		var t = this.tennic;
		var b = this.bottle;
		if(t.x+t.width>b.x&&t.x<b.x+b.width&&t.y+t.height>b.y&&t.y<b.y+b.width){
			return true;
		}
		return false;
	}

}